using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

using LocalBoard.Domain;

namespace LocalBoard.Persistence.SqlAccessor
{
	/// <summary>
	/// Summary description for SqlAttachmentAccessor.
	/// </summary>
	public class SqlAttachmentAccessor : SqlDataAccessBase, IAttachmentAccessor
	{
		public static readonly SqlAttachmentAccessor Instance = new SqlAttachmentAccessor();

		private SqlAttachmentAccessor()
		{
		}
		#region IAttachmentAccessor Members

		public ArrayList GetAttachmentListByMessageId(int messageId)
		{
			SqlDataReader dataReader = SqlHelper.ExecuteReader(ConnectionString, "GetAttachmentsByMessageId", messageId);

			ArrayList attachmentList = new ArrayList();
			while(dataReader.Read())
			{
				attachmentList.Add(new Attachment(DataConverter.GetInteger(dataReader["Id"]), DataConverter.GetString(dataReader["Name"]), DataConverter.GetInteger(dataReader["Size"]), DataConverter.GetString(dataReader["Path"])));
			}
			dataReader.Close();
			return attachmentList;
		}

		public int Insert(Attachment attachment, int messageId)
		{
			return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, "InsertAttachment", attachment.Name, attachment.Size, attachment.Path, messageId));
		}

		public void Delete(int id)
		{
			SqlHelper.ExecuteNonQuery(ConnectionString, "DeleteAttachment", id);
		}

		#endregion
	}
}
